//
//  StatusPictureView.swift
//  SinaWeiBo
//
//  Created by tuyue on 16/3/15.
//  Copyright © 2016年 xueyang.co.ltd. All rights reserved.
//

import UIKit

private let pictureCellId = "pictureCellId"
private let pictureCellMargin:CGFloat = 5

class StatusPictureView: UICollectionView {
    var imageURLs: [NSURL]?{
        didSet{
        //根据配图的数量，来确定视图的大小
        let pSize = calculatePictureViewSize()
            
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(pSize)
            }
            textLabel.text = "\(imageURLs?.count ?? 0)"
            //MARK: - 刷新集合视图
            reloadData()
        }
    }
    
    //MARK: - 重写构造方法
    init(){
        /**在初始化方法中创建流布局对象*/
    let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = pictureCellMargin
        layout.minimumLineSpacing = pictureCellMargin
        super.init(frame: CGRectZero, collectionViewLayout: layout)
        /**注册cell的可重用标识*/
       self.registerClass(PictureCell.self, forCellWithReuseIdentifier: pictureCellId)
        /**设置数据源*/
        self.dataSource = self
        self.backgroundColor = UIColor.whiteColor()
        /**setupUI*/
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 根据imageURLs的count，来计算cell的样式和size，以及cell中item的size
    private func calculatePictureViewSize() ->CGSize {
    let imageCount = imageURLs?.count ?? 0
        let maxWidth = ScreenWidth - 2 * StatusCellMargin
        let itemWidth = (maxWidth - 2 * pictureCellMargin)/3
        
        /**获取当前collectionView的流布局对象*/
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSizeMake(itemWidth, itemWidth)
        
        /**如果照片URL的数量为0，那么CGSize就返回CGSizeZero*/
        if imageCount == 0 {
        return CGSizeZero
        }
        
        /**imageCount为1的情况下 返回该尺寸*/
        if imageCount == 1 {
        let imageSize = CGSizeMake(150, 100)
            layout.itemSize = imageSize
            return imageSize
        }
        
        /**imageCount为4时候，返回该尺寸*/
        if imageCount == 4 {
        let w = itemWidth*2 + pictureCellMargin
            return CGSizeMake(w, w)
        }
        
        /**设置行数*/
        let row = CGFloat((imageCount-1)/3 + 1)
        /**求pictureView的高*/
        let pHeight = row * itemWidth + (row - 1) * pictureCellMargin
        
        return CGSizeMake(maxWidth, pHeight)
    }
    
    //MARK: - 设置UI
    private func setupUI(){
    addSubview(textLabel)
        textLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.center)
        }
    }
    
    //MARK: - 懒加载子视图
    private lazy var textLabel:UILabel = UIFactory.factoryLable("hello", fontSize: 40, textColor: UIColor.redColor())
}





 //MARK: - collectionView 的数据源方法
extension StatusPictureView: UICollectionViewDataSource {
    
   
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageURLs?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
     
        
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCellId, forIndexPath: indexPath) as! PictureCell
        
    cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.redColor() : UIColor.greenColor()
        
        cell.imageURL = imageURLs![indexPath.item]
        
        return cell
    }
    
    
    //MARK: - 创建PictureCell类
class PictureCell : UICollectionViewCell {
        
        var imageURL: NSURL?{
            didSet{
            iconView.sd_setImageWithURL(imageURL)
            }
        }
        
        /**重写PictureCell的初始化方法*/
        override init(frame: CGRect) {
            super.init(frame: frame)
            setupUI()
        }

        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        /**设置UI界面*/
        private func setupUI(){
        /**将图片添加到contentView*/
        contentView.addSubview(iconView)
            iconView.snp_makeConstraints { (make) -> Void in
                make.edges.equalTo(contentView.snp_edges)
            }
        }
        
        /**懒加载视图*/
    private lazy var iconView:UIImageView = {
    let iv = UIImageView()
        iv.contentMode = .ScaleAspectFill
        iv.clipsToBounds = true
        return iv
    }()
        
    }
}
